package cn.bblocks.cacheTest;


import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import springfox.documentation.service.Parameter;
import springfox.documentation.service.SecurityReference;

/**
 *
 * Swapper RESTful Api
 * Created by weijie_huang on 2017/9/6.
 * http://localhost:8089/cache-test/swagger-ui.html
 */

@Configuration
@EnableSwagger2

public class Swagger2  {
	private static final String CHITIC_BANK_API_PACKAGE = "cn.bblocks.cacheTest";

	@Bean
    public Docket platformApi() {

        return new Docket(DocumentationType.SWAGGER_2) 
        		.groupName("API接口文档")
        		.apiInfo(apiInfo())
        		//.forCodeGeneration(true)
                .select()
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
               
             //   .apis(RequestHandlerSelectors.any())
              .apis(RequestHandlerSelectors.basePackage(CHITIC_BANK_API_PACKAGE))
                .paths(PathSelectors.any())
                .build()
                
                //.globalOperationParameters(setHeaderToken())
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;


    }
//	 @Bean
//		public Docket createRestApi() {
//			return new Docket(DocumentationType.SWAGGER_2)
//					.apiInfo(apiInfo())
//					.select()
//					//.apis(RequestHandlerSelectors.basePackage("com.edu.intekids.*.controller"))
//					.apis(RequestHandlerSelectors.basePackage("com.edu.intekids"))
//					.paths(PathSelectors.any())
//					.build();
//		}
		
		

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList= new ArrayList();

        apiKeyList.add(new ApiKey("token","token", "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        //.forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[2];
        authorizationScopes[0] = authorizationScope;
        
        authorizationScopes[1] = new AuthorizationScope("token", "abcd");
        List<SecurityReference> securityReferences=new ArrayList<>();
        
        
        securityReferences.add(new SecurityReference("token", authorizationScopes));
        return securityReferences;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("API").description("©2019 Copyright. Powered By LUXCN.<br>"
        		+ "默认token:3d7784690f414bc37576ebb50ac79639")
                // .termsOfServiceUrl("")
                //.contact(new Contact("Starmark", "", "947618@163.com")).license("Apache License Version 2.0")
                //.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                .version("1.0").build();
    }
//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder().title("活动规则-API").description("全部接口都需要表单或者head送'token'字段<br>©2019 Copyright. Powered By .<br>"
//        		+ "")
//                // .termsOfServiceUrl("")
//                //.contact(new Contact("Starmark", "", "947618@163.com")).license("Apache License Version 2.0")
//                //.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
//                .version("1.0").build();
//    }
}
